package cn.paul.wechatshop.utils;

import java.math.BigDecimal;
import java.math.RoundingMode;

/**
 * Created by 徐凌峰 on 2017/10/1 0001.
 */
public class BigDecimalUtil {
    private BigDecimalUtil() {
    }

    public static BigDecimal add(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(v1));
        BigDecimal b2 = new BigDecimal(String.valueOf(v2));

        return b1.add(b2);
    }

    public static BigDecimal sub(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(v1));
        BigDecimal b2 = new BigDecimal(String.valueOf(v2));

        return b1.subtract(b2);
    }

    public static BigDecimal mul(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(v1));
        BigDecimal b2 = new BigDecimal(String.valueOf(v2));

        return b1.multiply(b2);
    }

    public static BigDecimal div(double v1, double v2) {
        BigDecimal b1 = new BigDecimal(String.valueOf(v1));
        BigDecimal b2 = new BigDecimal(String.valueOf(v2));
        //四舍五入保留两位小数
        return b1.divide(b2, 2, RoundingMode.HALF_UP);
    }

}
